/**
 * Copyright (c) 2019 qar All rights reserved.
 *
 * https://www.qar.io
 *
 * 版权所有，侵权必究！
 */
package com.example.controller;


import com.example.dto.SysNoticeDTO;
import com.example.model.api.Result;
import com.example.model.page.PageData;
import com.example.security.SecurityUser;
import com.example.service.SysNoticeService;
import com.example.service.SysNoticeUserService;
import com.example.validator.AssertUtils;
import com.example.validator.ValidatorUtils;
import com.example.validator.group.AddGroup;
import com.example.validator.group.DefaultGroup;
import com.example.validator.group.UpdateGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
* 通知管理
*
* @author Mark sunlightcs@gmail.com
*/
@RestController
@RequestMapping("notice")
@Tag(name = "通知管理")
public class SysNoticeController {
    @Autowired
    private SysNoticeService sysNoticeService;
    @Autowired
    private SysNoticeUserService sysNoticeUserService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('sys:notice:all')")
    public Result<PageData<SysNoticeDTO>> page(@RequestParam Map<String, Object> params){
        PageData<SysNoticeDTO> page = sysNoticeService.page(params);

        return new Result<PageData<SysNoticeDTO>>().ok(page);
    }

    @GetMapping("user/page")
    @Operation(summary = "获取被通知的用户")
    @PreAuthorize("hasAuthority('sys:notice:all')")
    public Result<PageData<SysNoticeDTO>> userPage(@RequestParam Map<String, Object> params){
        PageData<SysNoticeDTO> page = sysNoticeService.getNoticeUserPage(params);

        return new Result<PageData<SysNoticeDTO>>().ok(page);
    }

    @GetMapping("mynotice/page")
    @Operation(summary = "获取我的通知")
    public Result<PageData<SysNoticeDTO>> myNoticePage(@RequestParam Map<String, Object> params){
        PageData<SysNoticeDTO> page = sysNoticeService.getMyNoticePage(params);

        return new Result<PageData<SysNoticeDTO>>().ok(page);
    }

    @PutMapping("mynotice/read/{noticeId}")
    @Operation(summary = "标记我的通知为已读")
    public Result read(@PathVariable("noticeId") Long noticeId){
        sysNoticeUserService.updateReadStatus(SecurityUser.getUserId(), noticeId);

        return new Result();
    }

    @GetMapping("mynotice/unread")
    @Operation(summary = "我的通知未读数")
    public Result<Integer> unRead(){
        int count = sysNoticeUserService.getUnReadNoticeCount(SecurityUser.getUserId());

        return new Result<Integer>().ok(count);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('sys:notice:all')")
    public Result<SysNoticeDTO> get(@PathVariable("id") Long id){
        SysNoticeDTO data = sysNoticeService.get(id);

        return new Result<SysNoticeDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('sys:notice:all')")
    public Result save(@RequestBody SysNoticeDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        sysNoticeService.save(dto);

        return new Result();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('sys:notice:all')")
    public Result update(@RequestBody SysNoticeDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        sysNoticeService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('sys:notice:all')")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        sysNoticeService.delete(ids);

        return new Result();
    }

}